package edu.wuster.blog.service.impl;

import edu.wuster.blog.entity.Comment;
import edu.wuster.blog.entity.Reply;
import edu.wuster.blog.exception.MyException;
import edu.wuster.blog.mapper.BlogpartMapper;
import edu.wuster.blog.mapper.CommentMapper;
import edu.wuster.blog.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;



@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogpartMapper blogpartMapper;

    @Override
    public void addComment(Comment comment){
        //判断评论的博客是否还存在
        if (blogpartMapper.selectById(comment.getBlogId()) == null) {
            log.info("该博客不存在");
            throw new MyException("该博客不存在");
        }
        comment.setCreateTime(LocalDateTime.now());
        commentMapper.addComment(comment);

    }

    @Override
    public void addReply(Reply reply){

        reply.setCreateTime(LocalDateTime.now());
        commentMapper.addReply(reply);

    }

    //查看博客的评论
    @Override
    public List<Comment> selectByBlogId(Integer blogId){
        if (blogpartMapper.selectById(blogId) == null) {
            log.info("该博客不存在");
            throw new MyException("该博客不存在");
        }
        log.info("查看博客（id：{}）的评论", blogId);

        return commentMapper.selectByBlogId(blogId);

    }

    //根据评论id查看回复
    @Override
    public List<Reply> selectReply(Integer commentId){
        if (commentMapper.selectByCommentID(commentId) == null) {
            log.info("该评论不存在");
            throw new MyException("该评论不存在");
        }
        log.info("查看评论（id:{}）的回复", commentId);
        List<Reply> reply = commentMapper.selectReply(commentId);
        return reply;

    }

}
